#ifndef _BT_ANIMATION_H_
#define _BT_ANIMATION_H_

/*
 * Animation
 *
 * Frame by frame animation of textures
 */

#include <string>
#include "texture.h"
#include "xmlobject.h"

#define ANIM_DEFAULT	0x00
#define ANIM_FORWARD	0x00
#define ANIM_REVERSE	0x01
#define ANIM_LOOP		0x02
#define ANIM_PINGPONG	0x04

namespace bt
{
	class Animation : public XMLObject
	{
	public:
		Animation();
		
		Animation(Animation& rhs);
		
		Animation(const char* filename,
				  Vector2f& frameDims,
				  Uint8 numFramesX, Uint8 numFramesY,
				  Uint8 flags);
		
		Animation(Texture& tex,
				  Vector2f& frameDims,
				  Uint8 numFramesX, Uint8 numFramesY,
				  Uint8 flags);
		
		Animation(const char* filename,
				  Vector2f& frameDims, Vector2f& drawDims,
				  Uint8 numFramesX, Uint8 numFramesY,
				  Uint8 flags);
		
		Animation(Texture& tex,
				  Vector2f& frameDims, Vector2f& drawDims,
				  Uint8 numFramesX, Uint8 numFramesY,
				  Uint8 flags);
		
		~Animation();
		
		// Assignment Operator
		Animation& operator=(Animation& rhs);
		
		// Accessors
		Texture getTexture();
		
		Vector2f getDimensions();
		
		Uint32 getRefreshRate();
		
		Uint8 getFlags();
		
		Animation getSubAnimation(int startFrame, int endFrame);
		
		// Mutators
		void setTexture(Texture& tex,
						Vector2f& frameDims,
						Uint8 numFramesX, Uint8 numFramesY);
		
		void setTexture(Texture& tex,
						Vector2f& frameDims, Vector2f& drawDims,
						Uint8 numFramesX, Uint8 numFramesY);
		
		void setCurFrame(Uint8 frame);
		
		void setFrameBounds(Uint8 start, Uint8 end); 
		
		void setRefreshRate(Uint32 rate);
		
		void setFlags(Uint8 flags);
		
		// Actions
		void start();
		void stop();
		void reset();
		
		void draw();
		void draw(float x, float y);
		void freeLists();
		
		// Inherited from XMLObject
		bool loadFromXML(xmlDoc* doc, xmlNode* node);
		
		void writeToXML(xmlDoc* doc, xmlNode* node);
		
	protected:
		Texture texture;
		Vector2f frameDimensions;
		Uint8 numFramesX, numFramesY;
		GLuint frames;	/* Display list names, initial name */
		
		int curFrame;
		Uint8 numFrames, startFrame, endFrame;
		Vector2f dimensions;
		
		Uint32 refreshRate;	/* time for each frame in ms */
		SDL_TimerID timer;
		friend Uint32 UpdateAnimation(Uint32 interval, void* fakeThis);
		
		Uint8 flags;
	};
	
	Uint32 UpdateAnimation(Uint32 interval, void* fakeThis);
}

#endif /* _BT_ANIMATION_H_ */
